package com.csit.util;

/**
 * @author longweier
 * @Description: MD5算法工具类
 * @Copyright: 福州骏华科技信息有限公司 (c)2012
 * @Created Date : 2012-3-27
 * @vesion 1.0
 */
public class MD5Util {

	public static final int DIGEST_LENGTH = digest(MD5Util.class.getName()).length();

	private MD5Util() {
	}

	/**
	 * @param
	 * @param info
	 * @return
	 * @throws Exception
	 * @Description: 加密
	 * @Create: 2012-3-27 下午12:39:13
	 * @author longweier
	 * @update logs
	 */
	public static String digest(String info) {
		try {
			java.security.MessageDigest alga = java.security.MessageDigest.getInstance("MD5");
			alga.update(info.getBytes());
			byte[] digesta = alga.digest();
			return byte2hex(digesta);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * @param
	 * @param b
	 * @return
	 * @throws Exception
	 * @Description: 转换为16进制
	 * @Create: 2012-3-27 下午12:39:22
	 * @author longweier
	 * @update logs
	 */
	private static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) hs = hs + "0" + stmp;
			else hs = hs + stmp;
			if (n < b.length - 1) hs = hs + "";
		}
		return hs.toLowerCase();
	}

	public static void main(String[] args) {
		System.err.println(MD5Util.digest("251743270"));
	}

}
